/*
 * motor_drv.h
 *
 *  Created on: Oct 12, 2024
 *      Author: Jinming
 */

#ifndef INC_MOTOR_DRV_H_
#define INC_MOTOR_DRV_H_

#include "main.h"
#include "can.h"
#include "cmsis_os.h"

#define ENCODER_PLUSE_ROUND         131072L       //编码器一圈的脉冲数量

typedef enum
{
    CIA_NOT_READY_SWITCH_ON = 0,
    CIA_SWITCH_ON_DISABLE,
    CIA_READY_SWITCH_ON,
    CIA_SWITCH_ON,
    CIA_OPERATION_ENABLE,
    CIA_QUCIK_STOP,
    CIA_FAULT,
    CIA_FAULT_REACTION,
}CIA402_STATE_e;

typedef enum {
	MOTOR_MODE_IDLE = 0,			//未使用
	MOTOR_MODE_POS = 1,					// 位置模式
	MOTOR_MODE_RPM = 3,					// 转速模式
	MOTOR_MODE_TORQUE = 4,				// 转矩模式
	MOTOR_MODE_ORIGIN_HERE = 6,			//寻零模式
	MOTOR_MODE_POS_SYNC = 8,			//周期同步位置模式
	MOTOR_MODE_RPM_SYNC = 9,			// 周期同步转速模式
	MOTOR_MODE_TORQUE_SYNC = 10,			// 周期同步转矩模式
}MOTOR_MODE_e;					//电机控制模式

typedef struct
{
    float ratedTorque;          //额定力矩, Nm
    float ratedCurrent;			//额定电流, A
    float maxSpeed;             //最大转速, RPM
    int encoderResolution;      //编码器分辨率, 单圈脉冲数量
    float reductionRatio;
    float maxAcc;               //最大加速度,RPM/s
    float maxDec;               //最大减速度,RPM/s
    float stopDec;              //快停减速度,RPM/s
    float tffAccDec;            //转矩变化率,tff/s
    float torqueCoeff;			//转矩常数,Nm/A
}MotorAttr_t;           //电机特性

typedef struct
{
//    CANConfig can;
    CIA402_STATE_e curState;
    CIA402_STATE_e targetState;
    MOTOR_MODE_e curRunMode;			//当前控制模式
    uint32_t canId;
    MotorAttr_t tMotorAttr;

    osMutexId_t mutex;

    uint16_t errCode;

    int (*send)(uint32_t, uint8_t *, uint8_t);
    int (*receive)(CANMsgRx_t *, uint32_t);
    void (*clear)(void);			//清除接收缓冲区内容
}MotorUnit_t;

int motor_state_get(MotorUnit_t *motor, CIA402_STATE_e *state);
int motor_state_change(MotorUnit_t *motor, CIA402_STATE_e curState, CIA402_STATE_e targetState);
int motor_enter_speed_mode(MotorUnit_t *motor);
int motor_acc_set(MotorUnit_t *motor, float rpmps);
int motor_acc_get(MotorUnit_t *motor, float *acc);
int motor_dec_set(MotorUnit_t *motor, float rpmps);
int motor_dec_get(MotorUnit_t *motor, float *Dec);
int motor_quick_stop_dec_set(MotorUnit_t *motor, float rpmps);
int motor_quick_stop_dec_get(MotorUnit_t *motor, float *Dec);
int motor_jerk_set(MotorUnit_t *motor, float rpmpss);
int motor_jerk_get(MotorUnit_t *motor, float *jerk);
int motor_speed_set(MotorUnit_t *motor, float rpm);
int motor_speed_get(MotorUnit_t *motor, float *speed);
int motor_maxspeed_get(MotorUnit_t *motor, float *maxSpeed);
int motor_maxspeed_set(MotorUnit_t *motor, float maxSpeed);
int motor_position_set(MotorUnit_t *motor, float pos);
int motor_position_get(MotorUnit_t *motor, float *pos);
int motor_torque_set(MotorUnit_t *motor, float Nm);
int motor_torque_get(MotorUnit_t *motor, float *Nm);
int motor_current_get(MotorUnit_t *motor, float *ampere);
int motor_quickStop(MotorUnit_t *motor);
int motor_Idle(MotorUnit_t *motor);

int motor_mode_change(MotorUnit_t *motor, uint16_t mode);
MOTOR_MODE_e motor_curMode_get(MotorUnit_t *motor);

int motor_init(MotorUnit_t *motor, uint32_t id, MotorAttr_t *attr);
void motor_exit(MotorUnit_t *motor);

int motor_test(MotorUnit_t *motor);


#endif /* INC_MOTOR_DRV_H_ */
